﻿using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Ls.ImageSharep.App
{
    public partial class UCLove : UserControl
    {
        public UCLove()
        {
            InitializeComponent();
        }
        private void UCLove_Load(object sender, EventArgs e)
        {
            this.cbbmetatype.Items.AddRange(new string[] { "JPEG", "PNG", "GIF", "TGA","BMP" });
            this.cbbmetatype.SelectedIndex = 0;

            this.txtwidth.Text = "200";
            this.txtheight.Text = "200";
            this.txtquality.Text = "90";
        }
        private void btnupload_Click(object sender, EventArgs e)
        {
            //定义一个文件打开控件
            OpenFileDialog ofd = new OpenFileDialog();

            //设置打开对话框的标题
            ofd.Title = "请选择要打开的文件";
            //设置打开对话框可以多选
            ofd.Multiselect = false;
            //设置对话框打开的文件类型
            ofd.Filter = "图片文件|*.jpeg|图片文件|*.png|图片文件|*.jpg";
            //设置文件对话框当前选定的筛选器的索引
            ofd.FilterIndex = 2;
            //设置对话框是否记忆之前打开的目录
            ofd.RestoreDirectory = true;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                //获取用户选择的文件完整路径
                string filePath = ofd.FileName;
                //获取对话框中所选文件的文件名和扩展名，文件名不包括路径
                string fileName = ofd.SafeFileName;

                using (FileStream fsRead = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Read))
                {
                    ////定义二进制数组
                    //byte[] buffer = new byte[1024 * 1024 * 5];
                    ////从流中读取字节
                    //fsRead.Read(buffer, 0, buffer.Length);
                   
                    picboxleft.Image = System.Drawing.Image.FromStream(fsRead);

                    picboxleft.SizeMode = PictureBoxSizeMode.StretchImage;
                }
            }
        }

    

        private void btnsave_Click(object sender, EventArgs e)
        {
            int Quality =!string.IsNullOrEmpty(this.txtquality.Text)?Convert.ToInt32(this.txtquality.Text) : 90;
            //Stream memoryStream = new MemoryStream();

            //Stream myStream;
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();

            saveFileDialog1.Filter = "图片文件|*.jpeg|图片文件|*.png|图片文件|*.jpg|图片文件|*.gif";
            saveFileDialog1.FilterIndex = 1;
            saveFileDialog1.RestoreDirectory = true;

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                using (SixLabors.ImageSharp.Image image = SixLabors.ImageSharp.Image.Load(LsApp.ImgToByt(picboxleft.Image)))
                {

                    if (!string.IsNullOrEmpty(this.txtwidth.Text) && !string.IsNullOrEmpty(this.txtheight.Text))
                    {
                        image.Mutate(x => x.Resize(Convert.ToInt32(this.txtwidth.Text), Convert.ToInt32(this.txtheight.Text)).Crop(Convert.ToInt32(this.txtwidth.Text), Convert.ToInt32(this.txtheight.Text)));
                    }
                    var cbbmetatype = this.cbbmetatype.Text;


                    switch (cbbmetatype)
                    {

                        case "JPEG":
                            //image.Save(memoryStream, new SixLabors.ImageSharp.Formats.Jpeg.JpegEncoder()
                            //{
                            //    Quality = Quality
                            //});
                            image.Save(saveFileDialog1.FileName, new SixLabors.ImageSharp.Formats.Jpeg.JpegEncoder()
                            {
                                Quality = Quality
                            });
                            break;
                        case "PNG":
                            image.Save(saveFileDialog1.FileName, new SixLabors.ImageSharp.Formats.Png.PngEncoder());
                            break;
                        case "GIF":
                            image.Save(saveFileDialog1.FileName, new SixLabors.ImageSharp.Formats.Gif.GifEncoder());
                            break;
                        case "TGA":
                            image.Save(saveFileDialog1.FileName, new SixLabors.ImageSharp.Formats.Tga.TgaEncoder());
                            break;
                        case "Bmp":
                            image.Save(saveFileDialog1.FileName, new SixLabors.ImageSharp.Formats.Bmp.BmpEncoder());
                            break;
                        default:
                            image.Save(saveFileDialog1.FileName, new SixLabors.ImageSharp.Formats.Jpeg.JpegEncoder()
                            {
                                Quality = Quality
                            });
                            break;
                    }


                }
                //using (FileStream fsWrite = new FileStream(saveFileDialog1.FileName, FileMode.OpenOrCreate, FileAccess.Write))
                //{
                //    byte[] buffer = new byte[memoryStream.Length];
                //    memoryStream.Read(buffer, 0, buffer.Length);
                //    fsWrite.Write(buffer, 0, buffer.Length);

                //    fsWrite.Close();
                //}


            }
           

           
        }
    }
}
